package tako.wisers.com.android.apis.adapter;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.activity.LuckyDrawResultActivity;
import tako.wisers.com.android.apis.http.HttpGeter;
import tako.wisers.com.android.apis.item.StationItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class LuckyDrawAdapter extends BaseAdapter  {
	private ArrayList<StationItem> mItems;
	private Activity mCtx;
	public int prizeLevel = -1;

	public LuckyDrawAdapter(Activity c) {
		mCtx = c;
		mItems = new ArrayList<StationItem>();
	}

	public LuckyDrawAdapter(Activity c, ArrayList<StationItem> items) {
		mCtx = c;
		mItems = items;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		if (convertView == null) {
			// Make up a new view
			LayoutInflater inflater = (LayoutInflater) mCtx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.image_item, null);
		} else {
			// Use convertView if it is available
			view = convertView;
		}
		StationItem item = mItems.get(position);

		Bitmap image = item.getBitmap();
		ImageView iView = (ImageView) view.findViewById(R.id.image);
		if (image != null) {
			iView.setImageBitmap(image);
			iView.setBackgroundResource(R.drawable.picture_frame);
			iView.setVisibility(View.VISIBLE);
		} else {
			iView.setVisibility(View.GONE);
		}

		TextView t = (TextView) view.findViewById(R.id.title);
		t.setText(item.getTitle());

		t = (TextView) view.findViewById(R.id.owner);
		t.setText(item.getOwner());
		return view;
	}

	public void add(StationItem item) {
		mItems.add(item);
		notifyDataSetChanged();
	}

}
